# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#
package CCN::TimeOffset;
use strict; use warnings;

# time_offset: Attempt to compute the time offset (in hours) for a given timezone name.
sub time_offset($)
{
    my $tz_name = shift;

    my $time = time();
    # Default is to use the system difference, if anything.
    use Time::Local;
    our $time_offset = timelocal(localtime($time))-timegm(gmtime($time));

    # It seems that DateTime or DateTime::TimeZone are not readily available in (FC4?).
    # Do not overly penalize the user if they forgot to install it (the phones have their
    # own timezone management, at least for Cisco's and Polycom's).

    # try .. catch: http://www.perl.com/pub/a/2002/11/14/exception.html (Error.pm not in base Debian, at least)
    eval '
        use DateTime;
        use DateTime::TimeZone;

        my $tz = DateTime::TimeZone->new( name => $tz_name );

        my $dt = DateTime->now();
        $time_offset = $tz->offset_for_datetime($dt);
    ' if defined $tz_name;
    if($@)
    {
        warn "WARNING: You should consider installing DateTime::TimeZone. ($@)";
    }
    return $time_offset;
}

1;